/*
 * pps_bom
 */
package com.pitop.bom.ctrl;


import com.alibaba.fastjson.JSON;
import com.pitop.account.entity.Account;
import com.pitop.base.ctrl.BaseCtrl;
import com.pitop.bom.entity.Bom;
import com.pitop.bom.service.BomSVImpl;
import com.pitop.core.entity.BeanRet;
import com.pitop.core.entity.Page;
import com.pitop.core.exceptions.BaseException;
import com.pitop.core.tools.redis.RedisUtils;
import com.pitop.setting.service.SettingSVImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * bom 控制器
 *
 * @author berton
 */
@RestController
@RequestMapping("/bom")
@Slf4j
@Api(value = "bom控制器", description = "bom控制器")
public class BomCtrl extends BaseCtrl{

    @Resource
    protected RedisUtils redisUtils;
    @Autowired
    private BomSVImpl bomSVImpl;
    @Autowired
    private SettingSVImpl settingSVImpl;//设置


    /**
     * 查询Bom一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/load")
    @ResponseBody
    public BeanRet load(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Bom bom = bomSVImpl.load(id, code);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 查询Bom一个详情信息
     *
     * @param id
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "query")
    })
    @GetMapping(value = "/get")
    @ResponseBody
    public BeanRet get(Long id, String code) {
        if (id == null) {
            return null;
        }
        if (code == null) {
            return null;
        }
        Bom bom = bomSVImpl.get(id, code);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }


    /**
     * 根据条件id查询Bom一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/load/id/{id}")
    @ResponseBody
    public BeanRet loadById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Bom bom = bomSVImpl.loadById(id);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 根据条件code查询Bom一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/load/code/{code}")
    @ResponseBody
    public BeanRet loadByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Bom bom = bomSVImpl.loadByCode(code);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 根据条件id查询Bom一个详情信息
     *
     * @param id
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", dataType = "java.lang.Long", paramType = "path")
    })
    @GetMapping(value = "/get/id/{id}")
    @ResponseBody
    public BeanRet getById(@PathVariable Long id) {
        if (id == null || id == 0) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Bom bom = bomSVImpl.getById(id);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 根据条件code查询Bom一个详情信息
     *
     * @param code 编码
     * @return BeanRet
     */
    @ApiOperation(value = "查询Bom一个详情信息", notes = "查询Bom一个详情信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", dataType = "java.lang.String", paramType = "path")
    })
    @GetMapping(value = "/get/code/{code}")
    @ResponseBody
    public BeanRet getByCode(@PathVariable String code) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Bom bom = bomSVImpl.getByCode(code);
        log.info(JSON.toJSONString(bom));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 查询Bom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Bom信息集合", notes = "查询Bom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),

            @ApiImplicitParam(name = "createTimeBegin", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "createTimeEnd", value = "创建时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeBegin", value = "更新时间", paramType = "query"),
            @ApiImplicitParam(name = "updateTimeEnd", value = "更新时间", paramType = "query"),
    })
    @GetMapping(value = "/list")
    @ResponseBody
    public BeanRet list(@ApiIgnore Bom bom, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.list(bom, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.count(bom);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询Bom信息集合
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询Bom信息集合", notes = "查询Bom信息集合")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @GetMapping(value = "/list/by")
    @ResponseBody
    public BeanRet listByPk(Long id, String code, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.list(id, code, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.count(id, code);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 创建Bom
     * 1.参数合法性验证
     *
     * @return BeanRet
     */
    @ApiOperation(value = "创建Bom", notes = "创建Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PostMapping("/build")
    @ResponseBody
    public BeanRet build(@ApiIgnore Bom bom, HttpServletRequest request) {
        if (StringUtils.isEmpty(bom.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bom.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
         Account account  = this.loadAccount(request);
        bomSVImpl.save(bom, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }

    /**
     * 修改Bom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "修改Bom", notes = "修改Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query"),
            @ApiImplicitParam(name = "preCode", value = "上级编码", paramType = "query"),
            @ApiImplicitParam(name = "level", value = "子级级别", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom代码", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "名称", paramType = "query"),
            @ApiImplicitParam(name = "summary", value = "说明", paramType = "query"),
            @ApiImplicitParam(name = "ver", value = "版本", paramType = "query")
    })
    @PutMapping("/modify")
    @ResponseBody
    public BeanRet modify(@ApiIgnore Bom bom, HttpServletRequest request) {
        if (StringUtils.isEmpty(bom.getCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bom.getBomCode())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        if (StringUtils.isEmpty(bom.getName())) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        Account account  = this.loadAccount(request);
        bomSVImpl.modify(bom,account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, bom);
    }


    /**
     * 根据主键删除Bom
     *
     * @return BeanRet
     */
    @ApiOperation(value = "删除Bom", notes = "删除Bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "", paramType = "query"),
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query")
    })
    @DeleteMapping("/delete")
    @ResponseBody
    public BeanRet delete(Long id, String code) {
        if (id != null && StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomSVImpl.delete(id, code);
        return BeanRet.create(true, "删除Bom成功");
    }


    /**
     * 修改组件状态
     *
     * @param code  组件编码
     * @param state 状态
     * @return
     */
    @ApiOperation(value = "修改状态", notes = "修改状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "编码", paramType = "query", required = true),
            @ApiImplicitParam(name = "state", value = "状态(1032):Develop(开发) Stable(稳定) Stop(停用)", paramType = "query", required = true)
    })
    @PutMapping("/updateState")
    @ResponseBody
    public BeanRet updateState(String code, String state) {
        if (StringUtils.isEmpty(code) || StringUtils.isEmpty(state)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        bomSVImpl.updateState(code, state);
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 复制bom
     *
     * @param code bom编码
     * @return
     */
    @ApiOperation(value = "复制bom", notes = "复制bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "code", value = "bom编码", paramType = "query", required = true),
    })
    @GetMapping("/copy")
    @ResponseBody
    public BeanRet copy(String code, HttpServletRequest request) {
        if (StringUtils.isEmpty(code)) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
       Account account = this.loadAccount(request);
        bomSVImpl.copy(code, account.getAccount(), account.getCode());
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success);
    }

    /**
     * 查询未绑定成品的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询未绑定成品的bom", notes = "查询未绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query",required = true),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryUnBindProductBomList")
    @ResponseBody
    public BeanRet queryUnBindProductBomList(String productCode, String name, String bomCode, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.queryUnBindProductBomList(productCode, name, bomCode, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.countUnBindProductBomList(productCode, name, bomCode);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询已绑定成品的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询已绑定成品的bom", notes = "查询已绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "productCode", value = "成品编码", paramType = "query",required = true),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryBindProductBomList")
    @ResponseBody
    public BeanRet queryBindProductBomList(String productCode, String name, String bomCode, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.queryBindProductBomList(productCode, name, bomCode, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.countBindProductBomList(productCode, name, bomCode);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }


    /**
     * 查询未绑定项目的bom
     *
     * @return 分页对象
     */
    @ApiOperation(value = "查询未绑定成品的bom", notes = "查询未绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query",required = true),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryUnBindProjectBomList")
    @ResponseBody
    public BeanRet queryUnBindProjectBomList(String projectCode, String name, String bomCode, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.queryUnBindProjectBomList(projectCode, name, bomCode, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.countUnBindProjectBomList(projectCode, name, bomCode);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }

    /**
     * 查询已绑定成品的bom
     *
     * @param projectCode 项目编码
     * @param name        bom名称
     * @param bomCode     bom编码
     * @return
     */
    @ApiOperation(value = "查询已绑定成品的bom", notes = "查询已绑定成品的bom")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "curPage", value = "当前页", required = true, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", required = true, paramType = "query"),
            @ApiImplicitParam(name = "projectCode", value = "项目编码", paramType = "query",required = true),
            @ApiImplicitParam(name = "name", value = "bom名称", paramType = "query"),
            @ApiImplicitParam(name = "bomCode", value = "bom编码", paramType = "query"),
    })
    @GetMapping(value = "/queryBindProjectBomList")
    @ResponseBody
    public BeanRet queryBindProjectBomList(String projectCode, String name, String bomCode, @ApiIgnore Page<Bom> page) {
        if (page == null) {
            return BeanRet.create(BaseException.BaseExceptionEnum.Empty_Param);
        }
        List<Bom> boms = bomSVImpl.queryBindProjectBomList(projectCode, name, bomCode, page.getCurPage(), page.getPageSize());
        int total = bomSVImpl.countBindProjectBomList(projectCode, name, bomCode);
        page.setTotalRow(total);
        page.setVoList(boms);
        log.info(JSON.toJSONString(page));
        return BeanRet.create(true, BaseException.BaseExceptionEnum.Success, "", page);
    }
}
